package com.tencent.wxcloudrun.controller.AdminRole;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencent.wxcloudrun.model.AdminRole;
import com.tencent.wxcloudrun.response.R;
import com.tencent.wxcloudrun.service.AdminRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: RoleController
 * Package: com.tencent.wxcloudrun.controller
 * Description:
 *
 * @Date: 2023/1/22 8:34 AM
 * @Roleor: wht
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class AdminRoleController {

    @Autowired
    AdminRoleService adminRoleService;


    @PutMapping("/addOne")
    public <T> R<T> addOne(@RequestBody AdminRole adminRole) {
        boolean res = adminRoleService.save(adminRole);

        if (res) {
            return R.AddSuccess();
        } else {
            return R.AddFail();
        }
    }

    @DeleteMapping("/delOne")
    public <T> R<T> delOne(@RequestParam(required = true, value = "id") Integer id) {
        boolean res = adminRoleService.removeById(id);

        if (res) {
            return R.DelSuccess();
        } else {
            return R.DelFail();
        }
    }

    @PatchMapping("/updateOne")
    public <T> R<T> updateOne(@RequestBody AdminRole adminRole) {
        boolean res = adminRoleService.updateById(adminRole);

        if (res) {
            return R.UpdateSuccess();
        } else {
            return R.UpdateFail();
        }
    }

    @GetMapping("/getOne")
    public <T> R<T> getOne(@RequestParam(required = true, value = "id") Integer id) {
        AdminRole adminRole = adminRoleService.getById(id);
        return R.successData(adminRole);
    }

    @GetMapping("/getList")
    public <T> R<T> getList(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize
    ) {
        Page<AdminRole> page = new Page<>(pageNum, pageSize);
        List<AdminRole> list = adminRoleService.list(page);
        return R.successData(list);
    }
}
